package com.ruoyi.web.controller.blog;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.note.domain.NoteDaily;
import com.ruoyi.note.domain.NoteDocument;
import com.ruoyi.note.domain.NoteCategory;
import com.ruoyi.note.service.INoteDailyService;
import com.ruoyi.note.service.INoteDocumentService;
import com.ruoyi.note.service.INoteCategoryService;
import com.ruoyi.system.service.INoteLikeService;
import com.ruoyi.system.service.INoteFavoriteService;
import com.ruoyi.system.service.INoteCommentService;

/**
 * 博客笔记公开访问Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/blog/note")
public class BlogNoteController extends BaseController
{
    @Autowired
    private INoteDailyService noteDailyService;

    @Autowired
    private INoteDocumentService noteDocumentService;

    @Autowired
    private INoteCategoryService noteCategoryService;

    @Autowired
    private INoteLikeService noteLikeService;

    @Autowired
    private INoteFavoriteService noteFavoriteService;

    @Autowired
    private INoteCommentService noteCommentService;

    /**
     * 获取公开的日记列表
     */
    @Anonymous
    @GetMapping("/daily/list")
    public TableDataInfo listPublicDaily(@RequestParam(required = false) Long categoryId,
                                        @RequestParam(required = false) String tags,
                                        @RequestParam(required = false) String keyword)
    {
        startPage();
        NoteDaily noteDaily = new NoteDaily();
        noteDaily.setIsPublic("1"); // 只查询公开的笔记
        noteDaily.setStatus("0"); // 只查询正常状态的笔记
        if (categoryId != null) {
            noteDaily.setCategoryId(categoryId);
        }
        if (tags != null && !tags.isEmpty()) {
            noteDaily.setTags(tags);
        }
        if (keyword != null && !keyword.isEmpty()) {
            noteDaily.setTitle(keyword); // 可以在mapper中实现模糊查询
        }
        List<NoteDaily> list = noteDailyService.selectNoteDailyList(noteDaily);
        return getDataTable(list);
    }

    /**
     * 获取公开的文档列表
     */
    @Anonymous
    @GetMapping("/document/list")
    public TableDataInfo listPublicDocument(@RequestParam(required = false) Long categoryId,
                                           @RequestParam(required = false) String tags,
                                           @RequestParam(required = false) String docType,
                                           @RequestParam(required = false) String keyword)
    {
        startPage();
        NoteDocument noteDocument = new NoteDocument();
        noteDocument.setIsPublic("1"); // 只查询公开的文档
        noteDocument.setStatus("0"); // 只查询正常状态的文档
        if (categoryId != null) {
            noteDocument.setCategoryId(categoryId);
        }
        if (tags != null && !tags.isEmpty()) {
            noteDocument.setTags(tags);
        }
        if (docType != null && !docType.isEmpty()) {
            noteDocument.setDocType(docType);
        }
        if (keyword != null && !keyword.isEmpty()) {
            noteDocument.setTitle(keyword); // 可以在mapper中实现模糊查询
        }
        List<NoteDocument> list = noteDocumentService.selectNoteDocumentList(noteDocument);
        return getDataTable(list);
    }

    /**
     * 获取日记详情
     */
    @Anonymous
    @GetMapping("/daily/{noteId}")
    public AjaxResult getDailyInfo(@PathVariable("noteId") Long noteId)
    {
        NoteDaily noteDaily = noteDailyService.selectNoteDailyByNoteId(noteId);
        if (noteDaily == null || !"1".equals(noteDaily.getIsPublic()) || !"0".equals(noteDaily.getStatus())) {
            return error("笔记不存在或未公开");
        }
        
        // 增加阅读次数
        noteDailyService.increaseViewCount(noteId);
        
        // 获取统计信息
        Map<String, Object> result = new HashMap<>();
        result.put("note", noteDaily);
        result.put("likeCount", noteLikeService.countLikesByNote("daily", noteId));
        result.put("favoriteCount", noteFavoriteService.countFavoritesByNote("daily", noteId));
        result.put("commentCount", noteCommentService.countCommentsByNote("daily", noteId));
        
        return success(result);
    }

    /**
     * 获取文档详情
     */
    @Anonymous
    @GetMapping("/document/{documentId}")
    public AjaxResult getDocumentInfo(@PathVariable("documentId") Long documentId)
    {
        NoteDocument noteDocument = noteDocumentService.selectNoteDocumentByDocumentId(documentId);
        if (noteDocument == null || !"1".equals(noteDocument.getIsPublic()) || !"0".equals(noteDocument.getStatus())) {
            return error("文档不存在或未公开");
        }
        
        // 增加阅读次数
        noteDocumentService.increaseViewCount(documentId);
        
        // 获取统计信息
        Map<String, Object> result = new HashMap<>();
        result.put("note", noteDocument);
        result.put("likeCount", noteLikeService.countLikesByNote("document", documentId));
        result.put("favoriteCount", noteFavoriteService.countFavoritesByNote("document", documentId));
        result.put("commentCount", noteCommentService.countCommentsByNote("document", documentId));
        
        return success(result);
    }

    /**
     * 获取分类列表
     */
    @Anonymous
    @GetMapping("/category/list")
    public AjaxResult listCategories()
    {
        List<NoteCategory> list = noteCategoryService.selectNoteCategoryList(new NoteCategory());
        return success(list);
    }

    /**
     * 获取热门标签
     */
    @Anonymous
    @GetMapping("/tags/hot")
    public AjaxResult getHotTags(@RequestParam(defaultValue = "10") int limit)
    {
        // 这里可以实现获取热门标签的逻辑
        // 暂时返回空列表，后续可以根据需要实现
        return success();
    }

    /**
     * 搜索笔记
     */
    @Anonymous
    @GetMapping("/search")
    public AjaxResult searchNotes(@RequestParam String keyword,
                                    @RequestParam(required = false) String type)
    {
        Map<String, Object> result = new HashMap<>();
        
        if (type == null || "daily".equals(type)) {
            NoteDaily noteDaily = new NoteDaily();
            noteDaily.setIsPublic("1");
            noteDaily.setStatus("0");
            noteDaily.setTitle(keyword); // 在mapper中实现模糊查询
            List<NoteDaily> dailyList = noteDailyService.selectNoteDailyList(noteDaily);
            result.put("dailies", dailyList);
        }
        
        if (type == null || "document".equals(type)) {
            NoteDocument noteDocument = new NoteDocument();
            noteDocument.setIsPublic("1");
            noteDocument.setStatus("0");
            noteDocument.setTitle(keyword); // 在mapper中实现模糊查询
            List<NoteDocument> documentList = noteDocumentService.selectNoteDocumentList(noteDocument);
            result.put("documents", documentList);
        }
        
        return AjaxResult.success(result);
    }

    /**
     * 获取推荐笔记
     */
    @Anonymous
    @GetMapping("/recommend")
    public AjaxResult getRecommendNotes(@RequestParam(defaultValue = "5") int limit)
    {
        // 这里可以实现推荐算法
        // 暂时返回最新的公开笔记
        NoteDaily noteDaily = new NoteDaily();
        noteDaily.setIsPublic("1");
        noteDaily.setStatus("0");
        List<NoteDaily> dailyList = noteDailyService.selectNoteDailyList(noteDaily);
        
        NoteDocument noteDocument = new NoteDocument();
        noteDocument.setIsPublic("1");
        noteDocument.setStatus("0");
        List<NoteDocument> documentList = noteDocumentService.selectNoteDocumentList(noteDocument);
        
        Map<String, Object> result = new HashMap<>();
        result.put("dailyNotes", dailyList.size() > limit ? dailyList.subList(0, limit) : dailyList);
        result.put("documents", documentList.size() > limit ? documentList.subList(0, limit) : documentList);
        
        return success(result);
    }
}